import pandas as pd
from core.analyzer import DowTheoryAnalyzer
from core.backtester import Backtester
from data.loader import DataLoader
import argparse


def run_backtest(file_path, start_date=None, end_date=None):
    # 加载数据
    loader = DataLoader()
    data = loader.load(file_path)

    # 筛选时间范围
    if start_date and end_date:
        mask = (data.index >= start_date) & (data.index <= end_date)
        data = data.loc[mask].copy()

    # 执行分析
    analyzer = DowTheoryAnalyzer()
    result = analyzer.analyze(data)

    # 执行回测
    backtester = Backtester()
    performance = backtester.run_backtest(data, result.signals['Signal'])

    # 打印结果
    print("\n回测结果:")
    for k, v in performance['performance'].items():
        print(f"{k:>20}: {v}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("file", help="数据文件路径")
    parser.add_argument("--start", help="回测开始日期 (YYYY-MM-DD)", default=None)
    parser.add_argument("--end", help="回测结束日期 (YYYY-MM-DD)", default=None)
    args = parser.parse_args()

    run_backtest(args.file, args.start, args.end)